public class tribonacci {

    /**
     * 2024-8-3
     * 1. 求第 N 个泰波那契数
     * @param n
     * @return
     */
    public int tribonacci(int n) {
        //1. 创建 dp表
        int[] dp = new int[n + 1];

        //处理一下边界情况
        if (n == 0) return 0;
        if (n == 1 || n == 2) return 1;

        //2. 初始化
        dp[0] = 0;
        dp[1] = dp[2] = 1;

        //3. 填表
        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
        }

        //4. 返回值
        return dp[n];
    }


    /**
     * 利用滚动数组
     * 进行空间优化
     *
     * @param n
     * @return
     */
    public int tribonacci2(int n) {

        //处理一下边界情况
        if (n == 0) return 0;
        if (n == 1 || n == 2) return 1;

        int a = 0, b = 1, c = 1, d = 0;

        for (int i = 3; i <= n; i++) {
            d = a + b + c;
            //滚动操作
            a = b;
            b = c;
            c = d;
        }

        return d;
    }




    /**
     * 2. 三步问题
     * @param n
     * @return
     */
    public int waysToStep(int n) {
        int MOD = (int)1e9 + 7;

        //处理边界情况
        if(n == 1 || n == 2) return n;
        if(n == 3) return 4;

        int[] dp = new int[n+1];
        dp[1] = 1; dp[2] = 2; dp[3] = 4;
        for (int i = 4; i <= n; i++) {
            dp[i] = ((dp[i-1] + dp[i-2]) % MOD + dp[i-3]) % MOD;
        }
        return dp[n];
    }
}
